package com.linhnv.app.coverfollow.adapter;

import com.linhnv.app.coverfollow.R;
import com.linhnv.app.coverfollow.view.CoverFlow;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class ImageAdapter extends BaseAdapter {
	private Context mContext;
	int mGalleryItemBackground;
	private ImageView[] mImages;
	
	
	public ImageAdapter(Context context){
	mContext=context;
	
	mImages=new ImageView[mImageIds.length];
	createReflectedImages();
//	
	}
	
	
	private Integer[] mImageIds = {
			R.drawable.gallery_photo_1,
			R.drawable.gallery_photo_2,
			R.drawable.gallery_photo_3,
			R.drawable.gallery_photo_4,
			R.drawable.gallery_photo_5,
			R.drawable.gallery_photo_6,
			R.drawable.gallery_photo_7,
			R.drawable.gallery_photo_8
	};
	
	public ImageAdapter() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mImageIds.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ImageView i = new ImageView(mContext);
		//i.setImageResource(mImageIds[position]);
		i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
		i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		i.setImageDrawable(mImages[position].getDrawable());
		BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();		
		drawable.setAntiAlias(true);
		
		
		
		return i;
	}
	public float getScale(boolean focused, int offset) {
		return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
	}
	
	public boolean createReflectedImages(){
		final int reflectionGap=4;
		int index=0;
		for(int imageId:mImageIds){
			Log.e("Linhnv","======createReflectedImages");
			Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId);
			int width=originalImage.getWidth();
			int height=originalImage.getHeight();
			
			Matrix matrix= new Matrix();
			matrix.preScale(1, -1);
			
			Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false);
			Bitmap bitmapWithReflection = Bitmap.createBitmap(width
					, (height + height/2), Config.ARGB_8888);
			
			
			Canvas canvas= new Canvas(bitmapWithReflection);
			
			canvas.drawBitmap(originalImage, 0,0, null);
			
			Paint defaultPaint = new Paint();
			
			canvas.drawRect(0, height, width, height+reflectionGap, defaultPaint);
			
			canvas.drawBitmap(reflectionImage, 0, height+reflectionGap, null);
			
			Paint paint = new Paint();
			
			LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight()+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
			paint.setShader(shader);
			paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
			canvas.drawRect(0, height, width,bitmapWithReflection.getHeight() + reflectionGap, paint);
			
			ImageView imageView = new ImageView(mContext);
			imageView.setImageBitmap(bitmapWithReflection);
			imageView.setLayoutParams(new CoverFlow.LayoutParams(120, 180));
			imageView.setScaleType(ScaleType.MATRIX);
			mImages[index++]=imageView;
			
		}
		
		return true;
	}
}
